﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Data.Services.Client;

namespace ODataSource
{
    public class ODataSourceQueryableView : ContextDataSourceView
    {

        private ODataSource DataSource { get; set; }

        public ODataSourceQueryableView(DataSourceControl owner, string viewName, HttpContext context, ODataSource DataSource)
            : base(owner, viewName, context)
        {
            this.DataSource = DataSource;
        }

        protected override void HandleValidationErrors(IDictionary<string, Exception> errors, DataSourceOperation operation)
        {
            throw new NotImplementedException();
        }

        protected override ContextDataSourceContextData CreateContext(DataSourceOperation operation)
        {
            var context = Activator.CreateInstance(ContextType);
            var cd = new ContextDataSourceContextData(context);

            var es = (context as DataServiceContext).GetType().GetProperty(EntitySetName).GetValue(context, null);
            cd.EntitySet = es;
            return cd;
        }
        
        protected override Type GetEntitySetType()
        {
            return (Context as DataServiceContext).GetType().GetProperty(EntitySetName).GetValue(Context, null).GetType();
        }

        public override bool CanInsert
        {
            get
            {
                return DataSource.EnableInsert;
            }
        }

        public override bool CanUpdate
        {
            get
            {
                return DataSource.EnableUpdate;
            }
        }

        public override bool CanDelete
        {
            get
            {
                return DataSource.EnableDelete;
            }
        }

        protected override int InsertObject(object newEntity)
        {
            //return base.InsertObject(newEntity);
            
            var i = base.InsertObject(newEntity);
            //(Context as DataServiceContext).AddObject(EntitySetName, newEntity);
            var m = Context.GetType().GetMethod("AddTo" + EntitySetName);
            m.Invoke(Context, new object[]{newEntity});
            (Context as DataServiceContext).SaveChanges();
            return i;
        }

        protected override int DeleteObject(object oldEntity)
        {
            var i = base.DeleteObject(oldEntity);
            (Context as DataServiceContext).AttachTo(EntitySetName, oldEntity);
            (Context as DataServiceContext).DeleteObject(oldEntity);
            (Context as DataServiceContext).SaveChanges();
            return i;
        }

        protected override int UpdateObject(object oldEntity, object newEntity)
        {
            var i =  base.UpdateObject(oldEntity, newEntity);
            (Context as DataServiceContext).AttachTo(EntitySetName, newEntity);
            (Context as DataServiceContext).UpdateObject(newEntity);
            (Context as DataServiceContext).SaveChanges();
            return i;
        }

    }
}
